var goods=new Array();  //id集合
var tdFlag=0;   //标记一个tr已添加了多少个td
var trFlag=1;   //第n个tr
var trFull=4;   //tr元素上限
var tdID=0;     //td标记
var tableFull=9;    //table元素上限

var http_request;
function  createRequest(url) {
    http_request=false;
    if(window.XMLHttpRequest){      //非IE浏览器
        http_request=new XMLHttpRequest();      //创建XMLHttpRequest对象
    }else if(window.ActiveXObject){     //IE浏览器
        try {
            http_request=new ActiveXObject("Msxm12.XMLHTTP");      //创建XMLHttpRequest对象
        }catch (e){
            try{
                http_request=new ActiveXObject("Microsoft.XMLHTTP");     //创建XMLHttpRequest对象
            }catch (e){

            }
        }
    }
    if (!http_request){
        alert("不能创建XMLHttpRequest对象实例!");
        return false;
    }
    http_request.onreadystatechange=getRequest;        //调用返回结果处理函数
    http_request.open('GET',url,false);      //创建与服务器的链接
    http_request.send(null);        //向服务器发送请求
}

function getRequest(){
    if (http_request.readyState == 4){      //判断请求状态
        //alert("请求码："+http_request.status);
        if (http_request.status == 200){      //请求成功，开始处理返回结果
            alert("响应内容："+http_request.responseText);  //设置提示内容
        }else{
            alert("请求页面有错误！错误码为："+http_request.status);      //请求页面有错误
        }
    }
}

//通过ajax从后台获取列表
function addUpdateMessageToServlet(){
    alert(createUrl());
    //createRequest('AddWuliuServlet?'+createUrl());
}

//获得所有的id
function getIDs(){
    var list=new Array();
    for(var i=1;i<=tdID;i++){
        list.push(document.getElementById(("td"+i)).innerHTML);
    }
    var jsonStr=JSON.stringify(list);
    return jsonStr;
}

//获取标签内容构造url
function createUrl() {
    var zhuangtai=document.getElementById("huowuzhuangtai").innerHTML;
    if(zhuangtai=="其他"){
        zhuangtai=document.getElementById("other0").value;
    }
    var persionID=document.getElementById("persionID").value;
    var sheng=document.getElementById("sheng").innerHTML;
    if(sheng=="省份"){
        sheng="";
    }
    var shi=document.getElementById("shi").innerHTML;
    if(shi=="市/县"){
        shi="";
    }
    var qu=document.getElementById("qu").innerHTML;
    if(qu=="区"){
        qu="";
    }
    var other1=document.getElementById("other4").value;
    var shengT=document.getElementById("shengT").innerHTML;
    if(shengT=="省份"){
        shengT="";
    }
    var shiT=document.getElementById("shiT").innerHTML;
    if(shiT=="市/县"){
        shiT="";
    }
    var quT=document.getElementById("quT").innerHTML;
    if(quT=="区"){
        quT="";
    }
    var other2=document.getElementById("other5").value;
    var remark=document.getElementById("remark").value;
    var url="json="+getIDs()+"&zhuangtai="+zhuangtai+"&persionID="+persionID+"&sheng="+sheng+"&shi="+shi+"&qu="+qu
        +"&other1="+other1+"&shengT="+shengT+"&shiT="+shiT+"&quT="+quT+"&other2="+other2+"&remark="+remark;
    return url;
}

function selectZhuangtai(selectId,name) {
    document.getElementById("other0").value="";
    document.getElementById("sheng").innerHTML="省份";
    document.getElementById("shi").innerHTML="市/县";
    document.getElementById("qu").innerHTML="区";
    document.getElementById("other4").value="";
    document.getElementById("shengT").innerHTML="省份";
    document.getElementById("shiT").innerHTML="市/县";
    document.getElementById("quT").innerHTML="区";
    document.getElementById("other5").value="";

    document.getElementById("other5").innerHTML="省份";
    document.getElementById("div0").style.display="none";
    document.getElementById("div1").style.display="none";
    document.getElementById("div2").style.display="none";
    document.getElementById("div3").style.display="none";
    document.getElementById("div4").style.display="none";
    document.getElementById(selectId).innerText=name;
    if(name=="入库"){
        document.getElementById("div1").style.display="";
        document.getElementById("div2").style.display="";
    }else if(name=="出库"){
        document.getElementById("div3").style.display="";
        document.getElementById("div4").style.display="";
    }else if(name=="留仓"){
        document.getElementById("div1").style.display="";
        document.getElementById("div2").style.display="";
    }else if(name=="配送"){
        document.getElementById("div0").style.display="";
    }else{
        return;
    }
}

//检查订单是否存在
function checkDingdanID() {
    var goodId=document.getElementById("id").value;
    createRequest('AddWuliuServlet?code=1&dingdanID='+goodId);
}

//添加货单
function setIdOnClick() {
    var goodId=document.getElementById("id").value;
    goods.push(goodId);     //添加id到数组
    var table=document.getElementById("table");     //要添加子标签的标签
    var tr;
    //上一个tr已满或
    if(tdFlag==0){
        //alert("创建tr");
        tr=document.createElement("tr");        //生成<tr></tr>
        tr.setAttribute("id","tr"+(trFlag));
        var td=document.createElement("td");    //生成<td></td>
        tdID=tdID+1;
        td.setAttribute("id","td"+tdID);
        td.innerHTML=goodId;
        tr.appendChild(td);
        table.appendChild(tr);
    }else{
        //alert("检索tr");
        tr=document.getElementById("tr"+trFlag);
        var td=document.createElement("td");    //生成<td></td>
        tdID=tdID+1;
        td.setAttribute("id","td"+tdID);
        td.innerHTML=goodId;
        tr.appendChild(td);
    }
    tdFlag=tdFlag+1;
    //tr满了
    if(tdFlag==trFull){
        tdFlag=0;
        trFlag=trFlag+1;
    }
    if(trFlag%(tableFull+1)==0){
        document.getElementById("bt_add").setAttribute("disabled","disabled");
    }
}